package com.hghivln.erpwarehouse.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.erpwarehouse.system.common.lang.Constant;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import com.hghivln.erpwarehouse.system.entity.Role;
import com.hghivln.erpwarehouse.system.mapper.RoleMapper;
import com.hghivln.erpwarehouse.system.mapper.UserMapper;
import com.hghivln.erpwarehouse.system.service.RoleService;
import com.hghivln.erpwarehouse.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-07-01
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public DataGridlView queryAllRole(RoleVo roleVo) {
        Page<Role> page = new Page<>(roleVo.getPage(), roleVo.getLimit());
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.like(!StringUtils.isEmpty(roleVo.getName()), "name", roleVo.getName());
        getBaseMapper().selectPage(page, qw);
        return new DataGridlView(page.getTotal(), page.getRecords());
    }

    @Override
    public Role addRole(Role role) {
        role.setCreatetime(new Date());
        getBaseMapper().insert(role);
        return role;
    }

    @Override
    public Role updateRole(Role role) {
        getBaseMapper().updateById(role);
        return role;
    }

    @Override
    public List<Integer> queryMenuIdsByRid(Integer rid) {
        return getBaseMapper().queryMenuIdsByRid(rid);
    }

    @Override
    public void saveRoleMenu(Integer rid, Integer[] mids) {
        // 防止主键冲突
        getBaseMapper().deletRoleMenuById(rid);
        if (mids != null && mids.length > 0) {
            for (Integer mid : mids) {
                getBaseMapper().saveRoleMenu(rid, mid);
            }
        }
    }

    @Override
    public DataGridlView queryAllRoleNoPage() {
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        List<Role> list = getBaseMapper().selectList(qw);
        return new DataGridlView((long) list.size(), list);
    }


    @Override
    public boolean removeById(Serializable id) {
        getBaseMapper().removeUserRoleByRid(id);
        return super.removeById(id);
    }

    /**
     * 根据用户ID查询角色名称集合
     */
    @Override
    public List<String> queryRoleNamesByUserId(Integer id) {
        List<Integer> rids = userMapper.queryRoleIdsByUid(id);
        if (rids != null && rids.size() > 0) {
            QueryWrapper<Role> qw = new QueryWrapper<>();
            qw.eq("available",Constant.AVAILABLE_TRUE);
            qw.in("id",rids);
            List<Role> roles = getBaseMapper().selectList(qw);
            ArrayList<String> roleNames = new ArrayList<>();
            for (Role role : roles) {
                roleNames.add(role.getName());
            }
            return roleNames;
        } else {
            return null;
        }

    }
}
